package com.sevattal.service.leaves.controller;


import com.sevattal.common.base.result.R;
import com.sevattal.service.leaves.entity.Authority;
import com.sevattal.service.leaves.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/admin/leaves/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @ApiOperation("新增权限")
    @PostMapping("createAuth")
    public R createAuth(@ApiParam(value = "权限对象",required = true) @RequestBody Authority auth){
        boolean result = authorityService.createAuth(auth);
        if (result) {
            return R.ok().message("创建成功");
        }else {
            return R.error().message("创建失败");
        }
    }

    @ApiOperation("删除权限")
    @DeleteMapping("deleteAuth/{id}")
    public R deleteAuth(@ApiParam(value = "权限ID",required = true) @PathVariable String id){
        boolean result = authorityService.deleteAuth(id);
        if (result) {
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("更新权限")
    @PutMapping("updateAuth")
    public R  updateAuth(@ApiParam(value = "权限对象",required = true) @RequestBody Authority auth){
        boolean result = authorityService.updateAuth(auth);
        if (result) {
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取权限信息")
    @GetMapping("get/{id}")
    public R  getDept(@ApiParam(value = "权限ID",required = true) @PathVariable String id){
        Authority result = authorityService.getByAuthId(id);
        return R.ok().data("auth",result);
    }

    @ApiOperation("获取权限信息列表")
    @GetMapping("getList")
    public R  getAuthList(){
        List<Authority> result = authorityService.getAuthList();
        return R.ok().data("authList" ,result);
    }

}

